
//动态显示数码管设计
module Seg_Lut(
	input CLK_50M,
	input Rst_n,
	input [5:1] BUT,
	input [7:0] x,
	input [7:0] y,
	input [15:0] cout,
	output	reg	[5:0]	seg_nCS,	// 6个数码管的片选（LOW），LSB对应最左边
	output	reg	[7:0]	seg_leds	// 共阳，bit0点、1中间、2左上、3左下、4下、5右下、6右上、7上
	);

	//wire define
	wire   [3:0]              data0    ;        // x个位数
	wire   [3:0]              data1    ;        // x十位数
	wire   [3:0]              data2    ;        // y个位数
	wire   [3:0]              data3    ;        // y十位数
	wire   [7:0]              data4    ;        // cout个位数
	wire   [7:0]              data5    ;        // cout十位数
	wire   [7:0]              data6    ;
	
	//提取显示数值所对应的十进制数的各个位
	assign  data0 = x[3:0];       // 个位数
	assign  data1 = x[7:4];       // 十位数
	
	assign  data2 = y[3:0];       // 个位数
	assign  data3 = y[7:4];       // 十位数
	
	assign  data4 = cout[7:0];    // 个位数
	assign  data5 = cout[15:8];   // 十位数
	
	reg    [3:0] num_disp;        // 当前数码管显示的数据



	localparam			TIME2MS_CNT = 50000;
	reg					clk_2ms;		// 使用clk_2m处理
	integer				clk_cnt	;

	localparam			TIME1S_CNT = 2500_0000;
	reg					clk_1s;			// 使用clk_1s处理
	integer				clk_cnt_1s;

	
//得到周期为1s的时钟
always@(posedge CLK_50M or negedge Rst_n) begin
	if(!Rst_n) begin 
		clk_1s <= 1'b0;
		clk_cnt_1s <= 0;
	end
	else if(clk_cnt_1s == TIME1S_CNT-1) begin
		clk_cnt_1s <= 0;
		clk_1s <= !clk_1s;
	end
	else clk_cnt_1s <= clk_cnt_1s + 1;
end


// 得到周期为2ms的时钟
always @(posedge CLK_50M or negedge Rst_n)
begin
	if(!Rst_n) begin 
		clk_2ms <= 1'b0;
		clk_cnt <= 0;
	end
	else if(clk_cnt == TIME2MS_CNT-1) begin
		clk_cnt <= 0;
		clk_2ms <= !clk_2ms;
	end
	else clk_cnt <= clk_cnt + 1;
end

//数码管显示模块
always @(posedge clk_2ms or negedge Rst_n)	begin
	if(!Rst_n) begin
		seg_nCS  <= 6'b1111_11;
		seg_leds <= 8'hFF;
	end
	else if((!BUT[1] || !BUT[2] || !BUT[3] || !BUT[4] || !BUT[5]))begin 
		//数码管需要显示的数据，个人学号后6位334014
		if(!BUT[1]) begin
			case(seg_nCS)
				6'b1111_11: begin 
							seg_nCS  <= 6'b1111_10;		// 数码管，左1
							num_disp <= 4'd3;
							end
				6'b1111_10: begin
							seg_nCS  <= 6'b1111_01;		// 数码管，左2
							num_disp <= 4'd4;
							end
				6'b1111_01: begin
							seg_nCS  <= 6'b1110_11;		// 数码管，左3
							num_disp <= 4'd0;
							end
				6'b1110_11: begin
							seg_nCS  <= 6'b1101_11;		// 数码管，左4
							num_disp <= 4'd1;
							end
				6'b1101_11: begin
							seg_nCS  <= 6'b1011_11;		// 数码管，左5
							num_disp <= 4'd4;
							end
				6'b1011_11: begin
							seg_nCS  <= 6'b0111_11;		// 数码管，左6
							num_disp <= 4'd3;
							end
				default: 	begin
							seg_nCS  <= 6'b1111_11;
							end
			endcase	
		end
		if(!BUT[2]) begin//X值显示
			case(seg_nCS)
				6'b1111_11: begin 
							seg_nCS  <= 6'b1111_10;		// 数码管，左1
							num_disp <= data0;
							end
				6'b1111_10: begin
							seg_nCS  <= 6'b1111_01;		// 数码管，左2
							num_disp <= data1;
							end
				default:	seg_nCS  <= 6'b1111_11;
			endcase
		end
		if(!BUT[3]) begin//Y值显示
			case(seg_nCS)
				6'b1111_11: begin 
							seg_nCS  <= 6'b1111_10;		// 数码管，左1
							num_disp <= data0;
							end
				6'b1111_10: begin
							seg_nCS  <= 6'b1111_01;		// 数码管，左2
							num_disp <= data1;
							end
				6'b1111_01: begin
							seg_nCS  <= 6'b1110_11;		// 数码管，左3
							num_disp <= data2;
							end
				6'b1110_11: begin
							seg_nCS  <= 6'b1101_11;		// 数码管，左4
							num_disp <= data3;
							end
				default:	seg_nCS  <= 6'b1111_11;
			endcase
		end
		if(!BUT[4]) begin//Z值显示
			case(seg_nCS)
				6'b1111_11: begin
							seg_nCS  <= 6'b1110_11;		// 数码管，左3
							num_disp <= data5[3:0];
							end
				6'b1110_11: begin
							seg_nCS  <= 6'b1101_11;		// 数码管，左4
							num_disp <= data4[7:4];
							end
				6'b1101_11: begin
							seg_nCS  <= 6'b1011_11;		// 数码管，左5
							num_disp <= data4[3:0];
							end
				6'b1011_11: begin
							seg_nCS  <= 6'b0111_11;		// 数码管，左6
							num_disp <= data5[7:4];
							end
				default:	seg_nCS  <= 6'b1111_11;
			endcase
		end
		if(!BUT[5]) begin
			case(seg_nCS)
				6'b1111_11: begin
							seg_nCS  <= 6'b1111_10;
							num_disp <= data0;
							end
				6'b1111_10: begin
							seg_nCS  <= 6'b1111_01;
							num_disp <= data3;
							end			
				6'b1111_01: begin
							seg_nCS  <= 6'b1110_11;
							num_disp <= data2;
							end			
				6'b1110_11: begin
							seg_nCS  <= 6'b1101_11;
							num_disp <= data4[7:4];
							end		
				6'b1101_11: begin
							seg_nCS  <= 6'b1011_11;
							num_disp <= data4[3:0];
							end			
				6'b1011_11: begin
							seg_nCS  <= 6'b0111_11;
							num_disp <= data1;
							end		
				default:	seg_nCS  <= 6'b1111_11;
			endcase
		end
		if((!BUT[1] || !BUT[2] || !BUT[3] || !BUT[4] || !BUT[5])) begin
			case(num_disp)
				4'd0:  seg_leds <= 8'b0000_0011;		// 0
				4'd1:  seg_leds <= 8'b1001_1111;		// 1
				4'd2:  seg_leds <= 8'b0010_0101;		// 2
				4'd3:  seg_leds <= 8'b0000_1101;		// 3
				4'd4:  seg_leds <= 8'b1001_1001;		// 4
				4'd5:  seg_leds <= 8'b0100_1001;		// 5
				4'd6:  seg_leds <= 8'b0100_0001;		// 6
				4'd7:  seg_leds <= 8'b0001_1111;		// 7
				4'd8:  seg_leds <= 8'b0000_0001;		// 8
				4'd9:  seg_leds <= 8'b0000_1001;		// 9
				4'd10: seg_leds <= 8'b0001_1001;		// a
				4'd11: seg_leds <= 8'b1100_0001;		// b
				4'd12: seg_leds <= 8'b0110_0011;		// c
				4'd13: seg_leds <= 8'b1000_0101;		// d
				4'd14: seg_leds <= 8'b0110_0001;		// E
				4'd15: seg_leds <= 8'b0111_0001;		// F
				default: seg_leds <= 8'b0000_0011;		// 0
			endcase
		end
	end
	else begin
		seg_nCS  <= 6'b1111_11;
		seg_leds <= 8'hFF;
	end
end

endmodule
